<?php
/**
 * Created by PhpStorm.
 * User: root
 * Date: 11/14/18
 * Time: 3:52 PM
 */

namespace caroltc\jenkinsapp\controller;

use Exception;
use caroltc\jenkinsapp\library\DbHandler;
use caroltc\jenkinsapp\library\UserSession;

class User extends BaseController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 用户登录
     * @return null
     */
    public function login()
    {
        $this->checkParams(['username', 'password']);
        $username = $this->getRequestParam('username');
        $password = $this->getRequestParam('password');
        try {
            $DbHandler = new DbHandler();
            $exist_user = $DbHandler->getUserByPassword($username, $password);
            if (!$exist_user) {
                throw new Exception('账户密码错误');
            }

            // create session
            $session_data = (new UserSession())->createSession($exist_user['user_id']);
            $session_data['username'] = $exist_user['username'];
            $session_data['name'] = $exist_user['name'];
            $session_data['is_admin'] = $exist_user['is_admin'];
            return $this->responseOk(['result' => $session_data]);
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }
    }

    /**
     * 用户添加
     * @return null
     */
    public function addUser()
    {
        $this->checkUserSession();
        $this->checkUserAdmin();
        $this->checkParams(['username', 'password', 'name']);
        $username = $this->getRequestParam('username');
        $password = $this->getRequestParam('password');
        $name = $this->getRequestParam('name');
        try {
            $DbHandler = new DbHandler();
            $exist_user = $DbHandler->getUserByUsername($username);
            if ($exist_user) {
                throw new Exception('账户已存在');
            }

            // create user
            $user_id = $DbHandler->saveUser(['name' => $name, 'password' => $password, 'username' => $username]);
            if (!$user_id) {
                throw new Exception('创建失败');
            }
            return $this->responseOk(['result' => $user_id]);
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }
    }

    /**
     * 用户添加
     * @return null
     */
    public function getUserList()
    {
        $this->checkUserSession();
        $this->checkUserAdmin();
        try {
            $DbHandler = new DbHandler();
            return $this->responseOk(['result' => $DbHandler->getAllUser()]);
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }
    }

    /**
     * 用户添加
     * @return null
     */
    public function deleteUser()
    {
        $this->checkUserSession();
        $this->checkUserAdmin();
        $this->checkParams(['user_id']);
        $user_id = $this->getRequestParam('user_id');
        try {
            $DbHandler = new DbHandler();
            return $this->responseOk(['result' => $DbHandler->deleteUser($user_id)]);
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }
    }

    /**
     * 用户修改
     * @return null
     */
    public function updateUser()
    {
        $this->checkUserSession();
        $this->checkUserAdmin();
        $this->checkParams(['user_id']);
        $user_id = $this->getRequestParam('user_id');
        $name = $this->getRequestParam('name');
        $password = $this->getRequestParam('password');
        $status = $this->getRequestParam('status');
        try {
            $DbHandler = new DbHandler();
            $update_data= [];
            if (!empty($name)) {
                $update_data['name'] = $name;
            }
            if (!empty($password)) {
                $update_data['password'] = $password;
            }
            if (!empty($status)) {
                $update_data['status'] = $status;
            }
            if (empty($update_data)) throw new Exception('未修改');

            // update user
            $res = $DbHandler->update($user_id, $update_data);
            if (!$res) {
                throw new Exception('修改失败');
            }
            return $this->responseOk(['result' => $user_id]);
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }
    }

    /**
     * 用户自己修改
     * @return null
     */
    public function updateMyInfo()
    {
        $this->checkUserSession();
        $name = $this->getRequestParam('name');
        $password = $this->getRequestParam('password');
        try {
            $DbHandler = new DbHandler();
            $update_data= [];
            if (!empty($name)) {
                $update_data['name'] = $name;
            }
            if (!empty($password)) {
                $update_data['password'] = $password;
            }
            if (empty($update_data)) throw new Exception('未修改');

            // update user
            $res = $DbHandler->update($this->user_id, $update_data);
            if (!$res) {
                throw new Exception('修改失败');
            }
            return $this->responseOk(['result' => $res]);
        } catch (\Exception $e) {
            return $this->responseFail(50000, $e->getMessage(), ['request_data' => $this->getAllRequestParams()]);
        }
    }
}